package com.xbts.web.controller.cloud;

import com.xbts.common.annotation.Log;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.core.page.TableDataInfo;
import com.xbts.common.enums.BusinessType;
import com.xbts.system.domain.CnUserCloudServersRelation;
import com.xbts.system.domain.vo.CnUserCloudServersRelationVO;
import com.xbts.system.service.ICnUserCloudServersRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户-弹性云服务器关系Controller
 *
 * @author xbts
 * @date 2023-08-03
 */
@Api("用户-弹性云服务器关系")
@RestController
@RequestMapping("/system/relation")
public class CnUserCloudServersRelationController extends BaseController {

    @Autowired
    private ICnUserCloudServersRelationService cnUserCloudServersRelationService;

    /**
     * 查询用户-我的云服务器列表
     */
    @ApiOperation("查询用户-弹性云服务器关系列表")
    @PreAuthorize("@ss.hasPermi('system:relation:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody CnUserCloudServersRelation cnUserCloudServersRelation) {
//        startPage(cnUserCloudServersRelation);
        TableDataInfo dataTable = getDataTable(cnUserCloudServersRelationService.selectCnUserCloudServersRelationList(cnUserCloudServersRelation));
        dataTable.setTotal(cnUserCloudServersRelationService.selectCnUserCloudServersRelationCount(cnUserCloudServersRelation));
        return dataTable;
    }

    /**
     * 统计云服务器状态
     */
    @ApiOperation("统计云服务器状态")
    @PreAuthorize("@ss.hasPermi('system:relation:list')")
    @PostMapping("/statisticsStatus")
    public AjaxResult statisticsStatus(@RequestBody CnUserCloudServersRelation cnUserCloudServersRelation) {
        return success(cnUserCloudServersRelationService.statisticsStatus(cnUserCloudServersRelation));
    }

    /**
     * 获取用户-弹性云服务器关系详细信息
     */
    @ApiOperation("获取用户-弹性云服务器关系详细信息")
    @ApiImplicitParam(name = "userId", value = "用户-弹性云服务器关系ID", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermi('system:relation:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") String userId) {
        return success(cnUserCloudServersRelationService.selectCnUserCloudServersRelationByUserId(userId));
    }

    /**
     * 新增用户-弹性云服务器关系
     */
    @ApiOperation("新增用户-弹性云服务器关系")
    @PreAuthorize("@ss.hasPermi('system:relation:add')")
    @Log(title = "用户-弹性云服务器关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CnUserCloudServersRelation cnUserCloudServersRelation) {
        return toAjax(cnUserCloudServersRelationService.insertCnUserCloudServersRelation(cnUserCloudServersRelation));
    }

    /**
     * 修改用户-弹性云服务器关系
     */
    @ApiOperation("修改用户-弹性云服务器关系")
    @PreAuthorize("@ss.hasPermi('system:relation:edit')")
    @Log(title = "用户-弹性云服务器关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CnUserCloudServersRelation cnUserCloudServersRelation) {
        return toAjax(cnUserCloudServersRelationService.updateCnUserCloudServersRelation(cnUserCloudServersRelation));
    }

    @PutMapping("/remark")
    public AjaxResult editRemake(@RequestBody CnUserCloudServersRelation cnUserCloudServersRelation) {
        CnUserCloudServersRelation serversRelation = cnUserCloudServersRelationService.getById(cnUserCloudServersRelation.getId());
        serversRelation.setRemark(cnUserCloudServersRelation.getRemark());
        return toAjax(cnUserCloudServersRelationService.updateCnUserCloudServersRelation(serversRelation));
    }
    /**
     * 删除用户-弹性云服务器关系
     */
    @ApiOperation("删除用户-弹性云服务器关系")
    @ApiImplicitParam(name = "userIds", value = "用户-弹性云服务器关系ID集合", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermi('system:relation:remove')")
    @Log(title = "用户-弹性云服务器关系", businessType = BusinessType.DELETE)
    @PostMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<String> userIds) {
        return toAjax(cnUserCloudServersRelationService.deleteCnUserCloudServersRelationByUserIds(userIds));
    }

    /**
     * 购买相同配置
     */
    @ApiOperation("购买相同配置")
    @ApiImplicitParam(name = "userId", value = "用户-弹性云服务器关系ID", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermi('system:relation:query')")
    @GetMapping(value = "/buyAgain/{cloudServersId}")
    public AjaxResult buyAgain(@PathVariable("cloudServersId") String cloudServersId) {
        cnUserCloudServersRelationService.buyAgain(cloudServersId);
        return success();
    }
}
